package cn.edu.ctbu.myfirstweb.controller;

import cn.edu.ctbu.myfirstweb.domain.Administrator;
import cn.edu.ctbu.myfirstweb.domain.Student;
import cn.edu.ctbu.myfirstweb.domain.Teacher;
import cn.edu.ctbu.myfirstweb.service.AdministratorService;
import cn.edu.ctbu.myfirstweb.service.StudentService;
import cn.edu.ctbu.myfirstweb.service.TeacherService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@Slf4j
public class LoginController {

    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    AdministratorService administratorService;

    /**
     * @description 通过request读取session中的数据来获得学生用户信息并将其转json格式
     * @param request
     * @param response
     * @author HighJ
     */
@PostMapping("/getuser")
    public void getuser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Student student = (Student) request.getSession().getAttribute("SYSTEM_USER_SESSION");
        if (student != null) {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.writeValue(response.getOutputStream(), student);
        }
    }

    /***
     * 登录验证，若验证通过则创建session和cookie
     * @param request
     * @param session
     * @param response
     * @author HighJ
     */
    @PostMapping("/result")
    public String result(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
        Long uNum = Long.valueOf(request.getParameter("username"));
        String password = request.getParameter("password");
        String option = request.getParameter("inlineRadioOptions");//登录人员类型
        //登录用户为学生
        if (option.equals("option2")) {
            Student student = studentService.login(uNum, password);//根据输入的账号密码来验证登录
            if (student != null) {
                // 将登录用户信息保存到session中
                session.setAttribute("SYSTEM_USER_SESSION", student);
                // 保存cookie，实现自动登录
                Cookie cookie_username = new Cookie("cookie_username", String.valueOf(student.getNum()));//此处可能出错，因为保存的不是账号而是name
                // 设置cookie的持久化时间，30天
                cookie_username.setMaxAge(30 * 24 * 60 * 60);
                // 设置为当前项目下都携带这个cookie
                cookie_username.setPath(request.getContextPath());
                // 向客户端发送cookie
                response.addCookie(cookie_username);
                return "/Html/student/stu";
            } else {
                return "/Html/error";
            }
        }
        //登录用户为教师
        else if (option.equals("option1")) {
            Teacher teacher = teacherService.queryByTidAndPassword(uNum, password);//根据工号和密码查找教师
            if (teacher != null) {
                // 将登录用户信息保存到session中
                session.setAttribute("SYSTEM_USER_SESSION", teacher);
                // 保存cookie，实现自动登录
                Cookie cookie_username = new Cookie("cookie_username", String.valueOf(teacher.getTid()));
                // 设置cookie的持久化时间，30天
                cookie_username.setMaxAge(30 * 24 * 60 * 60);
                // 设置为当前项目下都携带这个cookie
                cookie_username.setPath(request.getContextPath());
                // 向客户端发送cookie
                response.addCookie(cookie_username);
                return "/Html/teacher/teacher";
            } else {
                return "/Html/error";
            }
        }
        //用户为管理员
        else if (option.equals("option3")) {
            Administrator administrator = administratorService.login(uNum, password);//根据工号和密码查找教师
            if (administrator != null) {
                // 将登录用户信息保存到session中
                session.setAttribute("SYSTEM_USER_SESSION", administrator);
                // 保存cookie，实现自动登录
                Cookie cookie_username = new Cookie("cookie_username", String.valueOf(administrator.getId()));
                // 设置cookie的持久化时间，30天
                cookie_username.setMaxAge(30 * 24 * 60 * 60);
                // 设置为当前项目下都携带这个cookie
                cookie_username.setPath(request.getContextPath());
                // 向客户端发送cookie
                response.addCookie(cookie_username);
                return "/Html/administrator/menu";
            } else {
                return "/Html/error";
            }
        }

        return "/Html/error";

    }

    /**
     * 退出登录同时删除session和cookie
     * @param request
     * @param response
     * @author HighJ
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 删除session里面的用户信息
        request.getSession().removeAttribute("SYSTEM_USER_SESSION");
        //创建一个新的空cookie
        Cookie cookie_username = new Cookie("cookie_username", "");
        // 设置cookie的持久化时间，0
        cookie_username.setMaxAge(0);
        // 设置为当前项目下都携带这个cookie
        cookie_username.setPath("/");
        // 向客户端发送cookie,使用新的空cookie代替原来的cookie，即在推出登陆后删除cookie
        response.addCookie(cookie_username);
        return "/Html/login";
    }
}
